package com.bupt.cosmfl.cosmflweb.constant;

import lombok.Data;

/**
 * 通用的响应体
 */
@Data
public class ResponseResult<T> {
    /**
     * 响应码
     */
    private Integer code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应的对象
     */
    private T data;

    protected ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public static <T> ResponseResult<T> ok(T data) {
        return new ResponseResult<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
    }

    public static <T> ResponseResult<T> ok(T data, String msg) {
        return new ResponseResult<>(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> ResponseResult<T> error(Integer code, String msg) {
        return new ResponseResult<>(code, msg, null);
    }

    public static <T> ResponseResult<T> error() {
        return ResponseResult.error(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMessage());
    }

    public static <T> ResponseResult<T> error(String msg) {
        return ResponseResult.error(ResponseCode.ERROR.getCode(), msg);
    }
}
